package com.deyuanyun.pic.settings.controller.org;

import com.deyuanyun.pic.common.util.RequestUtil;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.settings.controller.dto.org.CompanyDTO;
import com.deyuanyun.pic.settings.controller.dto.org.CompanyVO;
import com.deyuanyun.pic.settings.domain.base.Attachment;
import com.deyuanyun.pic.settings.service.base.AttachmentService;
import com.deyuanyun.pic.settings.service.org.CompanyService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * 
 * 
 * 公司管理
 *
 * @author    王敏
 * @version   2015年12月15日上午10:28:07
 * Method List:                       
 *				toPage 进入公司管理 列表界面
 *				search 查询数据
 *				save   添加数据
 *				update 修改数据
 *				del    删除数据
 */
@Controller
@RequestMapping("/settings/org/company")
public class CompanyController {

	@Resource
	private CompanyService companyService;

    @Resource
    private AttachmentService attachmentService;
	
	@RequestMapping(value="/page.html",method=RequestMethod.GET)
	public ModelAndView toPage( ModelAndView mav)
	{
		mav.setViewName("/settings/org/company");
		return mav;
	}

	/**
	 * 查询公司对应的logo
	 * @param id 公司id
	 * @return 
	 * @author axiao(xj08.dream@gmail.com)
	 * @date 2016-11-10
	 */
	@RequestMapping("/queryLogo.json")
	@ResponseBody
	public void queryLogo(String id) {
        Attachment atta =  attachmentService.queryAttachmentByCustomOne(id, "companyLogo");
        AjaxSupport.sendSuccessText("success", atta == null ? new Attachment() : atta, "id", "path", "businessId");
	}
    
    /**
     * 查询公司列表信息
     * @param companyVO
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-11-11
     */
	@RequestMapping("/searchInfo.json")
	@ResponseBody
	public void searchInfo(CompanyVO companyVO)
	{
        List<CompanyDTO> companyDTOs = companyService.queryAll(companyVO);
		AjaxSupport.sendSuccessText("success",companyDTOs);
	}

	@RequestMapping(value="/add.json",method=RequestMethod.POST)
	@ResponseBody
	public  void save(CompanyVO companyVO)
	{
		companyService.saveInfo(companyVO);
		AjaxSupport.sendSuccessText("success", "保存成功!");
	}

	@RequestMapping(value="/updateState.json",method=RequestMethod.POST)
	@ResponseBody
	public void updateState( HttpServletRequest request)
	{
		HashMap param= RequestUtil.parseRequestParamToHashMap(request);
        companyService.updateState(param);
		AjaxSupport.sendSuccessText("success", "");
	}
	
	@RequestMapping(value="/update.json",method=RequestMethod.POST)
	@ResponseBody
	public void update(CompanyVO companyVO)
	{
		companyService.updateInfo(companyVO);
		AjaxSupport.sendSuccessText("success", "更新成功!");
	}
	
	@RequestMapping(value="/del.json",method=RequestMethod.POST)
	@ResponseBody
	public void del(@RequestParam(value = "idList") List<String> idList)
	{
        int n = companyService.deleteInfo(idList);
		AjaxSupport.sendSuccessText(Integer.toString(n), "删除成功！");
	}
}
